<?php

class Pages {
    //put your code here
    static public function insert_page(){
    //$page_id=POST_value('page_id','');   
        global $dbdefault;
        $page_title=POST_value('page_title','');   
        $page_description=POST_value('page_description','');   
        $page_content=POST_value('page_content','');  
        //

        $stats=validate_page($page_title,$page_content);
        if (!$stats['success']){
            return array('success'=>false,'message'=>$stats['message']);
        }else{
            //echo 'Inserting...';
            $query="INSERT INTO PAGES (PAGE_CONTENT, PAGE_TITLE, PAGE_DESCRIPTION) 
                    VALUES ($page_content,$page_title, $page_description)";
            //$binds=array(':page_id'=>$page_id, ':page_title'=> $page_title,':page_description'=>$page_description,':page_content'=>$page_content);
            $res = mysql_query($query,$dbdefault);
            $page_id=mysql_insert_id();
            //echo oci_num_rows($res);
            return array('success'=>true,'page_id'=>$page_id);
        }
}
static public function delete_page(){
    global $dbdefault;
    $page_id=GET_value('page_id','');   
    $query=sprintf("DELETE FROM PAGES WHERE PAGE_ID=%s",  quote_smart($page_id));
    $res=  mysql_query($query,$dbdefault) or die('Something wrong!');
    return true;
}

static public function update_page(){
    $page_id=GET_value('page_id','');   
    $page_title=POST_value('page_title','');   
    $page_description=POST_value('page_description','');   
    $page_content=POST_value('page_content','');  
    //
    $stats=validate_page($page_id,$page_title,$page_content);
    if (!$stats['success']){
        return array('success'=>false,'message'=>$stats['message']);
    }else{
        //echo 'Inserting...';
        $query="UPDATE PAGES SET PAGE_CONTENT=:page_content, PAGE_TITLE=:page_title, 
                     PAGE_DESCRIPTION=:page_description WHERE PAGE_ID=:page_id";  
        $binds=array(':page_id'=>$page_id, ':page_title'=> $page_title,
                     ':page_description'=>$page_description,':page_content'=>$page_content);
        $res = oci_binded_query($query, $binds);
        //echo oci_num_rows($res);
        return array('success'=>true,'page_id'=>$page_id);
    }
}
    
static public function validate_page($page_title,$page_content){
        $errs=0;
        $err_msg='';
        if (empty($page_title)){
            $err_msg.='<p>Thiếu tiêu đề trang!</id>'; $errs++;
        }
        if (empty($page_content)){
            $err_msg.='<p>Thiếu nội dung trang!</id>'; $errs++;
        }

        if ($errs){
            return array('success'=>false,'message'=>$err_msg);
        }else{
            return array('success'=>true);        
        }
}


}

?>
